<?php

namespace App\Http\Controllers;
use GuzzleHttp\Client;
use Illuminate\Routing\Controller as BaseController;
class ApiController extends BaseController
{
    const ERROR_CODE = 400; //一般错误code
    const SUC_CODE = 200; //请求成功code
    public function __construct()
    {
        error_reporting(E_ERROR);

    }

    public static function response($data = null,$ret='200',$msg='操作成功')
    {

        if (!empty($data)&&!is_object($data)&&!is_array($data)){
            throw new  \ErrorException('data类型错误');
        }

        if (empty($data)) {
            if (is_object($data)){
                $res =  ['ret'=>intval($ret),'msg'=>$msg,'data'=>new \stdClass()];
            }else{
                $res =  ['ret'=>intval($ret),'msg'=>$msg,'data'=>[]];
            }

        } else {
            $res =  ['ret'=>intval($ret),'msg'=>$msg,'data'=>$data];
        }
        if (!empty($res['data']) && is_array($res['data'])) {
            foreach ($res['data'] as  &$val) {
                if ( is_numeric ( $val ) ) {
                    $val = strval($val);
                }
                if (is_null($val)) {
                    $val = '';
                }
                if (is_array ($val)) {
                    $val = self::changeString($val);
                }
            }
        }
        return response(json_encode($res,JSON_UNESCAPED_UNICODE))
            ->header('Content-Type', 'application/json')
            ->header('Charset','UTF-8')
            ->header('Access-Control-Allow-Credentials', 'false');
    }

    public static function changeString($array)
    {
        foreach ($array as  $key=>$val) {
            if ( is_numeric ( $val ) ) {
                $array[$key] = strval($val);
            }
            if (is_null($val)) {
                $array[$key] = '';
            }
            if (is_array($val)){
                $array[$key] = self::changeString($val);
            }
        }
        return $array;
    }


    public static function Token(){
        $str = md5(uniqid(md5(microtime(true)),true));  //生成一个不会重复的字符串
        $str = sha1($str);  //加密
        return $str;
    }


    public static function mkPass($user_password,int $user_salt)
    {
        $password = md5( $user_password .$user_salt);
        return $password;
    }


    public static function http_json($params,$url)
    {
        if (empty($url)){
            throw new \Error('URL为空');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_POST,           1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($params));// 必须为字符串
        curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/plain'));// 必须声明请求头
        $return = curl_exec($ch);
        return $return;
    }


    public static function get_order_sn(){
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        return $orderSn;
    }



    /**
     * 时间轴函数, Unix 时间戳
     * @param int $time 时间
     */
    public static function TranTime($time) {
        //$time = strtotime($time);
        $nowTime = time ();
        $message = '';
        //一年前
        if (idate ( 'Y', $nowTime ) != idate ( 'Y', $time )) {

            if ((date('m',$nowTime)==date('m',$time))&&(date('m',$nowTime)==date('m',$time))) {
                $message = date ( 'Y年', $time ).'的今天';
            }else{
                $message = date ( 'Y年m月d日', $time );
            }
        }
        else {
            //同一年
            $days = idate ( 'z', $nowTime ) - idate ( 'z', $time );
            switch(true){
                //一天内
                case (0 == $days):
                    $seconds = $nowTime - $time;
                    //一小时内
                    if ($seconds < 3600) {
                        //一分钟内
                        if ($seconds < 60) {
                            if (3 > $seconds) {
                                $message = '刚刚';
                                break;
                            } else {
                                $message = $seconds . '秒前';
                                break;
                            }
                        }
                        $message = intval ( $seconds / 60 ) . '分钟前';
                        break;
                    }
                    $message = idate ( 'H', $nowTime ) - idate ( 'H', $time ) . '小时前';
                    break;
                //昨天
                case (1 == $days):
                    $message = '昨天' . date ( 'H:i', $time );
                    break;
                //前天
                case (2 == $days):
                    $message = '前天 ' . date ( 'H:i', $time );
                    break;
                //7天内
                case (7 > $days):
                    $message = $days . '天前';
                    break;
                //超过7天
                default:
                    $message = date (  'n月j日 H:i', $time );
                    break;
            }
        }
        return $message;
    }
}
